package ${package}.service.sil.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.imydao.common.core.exception.ServiceException;
import com.imydao.common.core.utils.OrikaUtils;
import com.imydao.common.core.utils.PageUtils;
import ${package}.domain.sil.${ClassName};
import ${package}.domain.sil.dto.${ClassName}Dto;
import ${package}.domain.sil.dto.${ClassName}SearchDto;
import ${package}.domain.sil.vo.${ClassName}Vo;
import ${package}.mapper.sil.${ClassName}Mapper;
import ${package}.service.sil.${ClassName}Service;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * ${tableComment}Service实现
 *
 * @author ${author} ${email}
 * @since ${version} ${date}
 */
@Slf4j
@Service
@AllArgsConstructor
public class ${ClassName}ServiceImpl extends ServiceImpl<${ClassName}Mapper, ${ClassName}> implements ${ClassName}Service {

    private final ${ClassName}Mapper ${className}Mapper;

    @Override
    public List<${ClassName}> findList(${ClassName}SearchDto dto) {
        PageUtils.startPage();
        List<${ClassName}> list = ${className}Mapper.selectByWhere(dto);
        return list;
    }

    @Override
    public Long add(${ClassName}Dto dto) {
        //1、请求参数转换为实体对象
        ${ClassName} entity = OrikaUtils.convert(dto, ${ClassName}.class);
        //2、对象入库
        boolean saveResult = this.save(entity);
        log.info("对象新增入库返回：{}", saveResult);
        return entity.getId();
    }

    @Override
    public Integer edit(${ClassName}Dto dto) {
        //1、查询实体对象
        ${ClassName} entity = this.getById(dto.getId());
        if (entity == null) {
            throw new ServiceException("object.not.exist");
        }
        //2、更新实体对象，忽略null值
        BeanUtil.copyProperties(dto, entity, CopyOptions.create().setIgnoreNullValue(true));
        //3、对象更新入库
        boolean updateResult = this.updateById(entity);
        log.info("对象更新入库返回：{}", updateResult);
        return updateResult ? 1 : 0;
    }

    @Override
    public ${ClassName}Vo detail(Long id) {
        //1、查询实体对象
        ${ClassName} entity = this.getById(id);
        if (entity == null) {
            throw new ServiceException("object.not.exist");
        }
        //2、构造返回对象
        return OrikaUtils.convert(entity, ${ClassName}Vo.class);
    }

    @Override
    public Integer delete(Long[] ids) {
        // 逻辑删除
        boolean removeResult = this.removeBatchByIds(Arrays.asList(ids));
        log.info("对象删除入库返回：{}", removeResult);
        return removeResult ? ids.length : 0;
    }

}